package com.cqc.jdk8.Jutil.stream.collectors;

import com.cqc.eb.TpDefEB;
import com.cqc.eb.UserEB;
import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class GroupByTest {


    List<UserEB> userEBList = null;
    List<UserEB> userEBList2 = null;
    List<TpDefEB> tpDefList = null;

    @Before
    public void before() {
        this.getUserEBList();
        this.getUserEBList2();
        this.getTpDefEBList();

    }


    /**
     * 按照年龄分组
     */
    @Test
    public void f1() {
        Map<Integer, List<UserEB>> map = userEBList.stream()
                .collect(Collectors.groupingBy(UserEB::getAge));

        map.entrySet().stream()
                .forEach(System.out::println);
        /**结果：
         * 28=[UserEB{name='古力娜扎', age=28}]
         * 30=[UserEB{name='刘诗诗', age=30}, UserEB{name='刘亦菲', age=30}]
         */
    }

    /**
     * 字典翻译  list>Map<String, Map<String, String>>
     * for循环写法
     */
    @Test
    public void f2() {
        Map<String, List<TpDefEB>> groupList = tpDefList.stream()
                .collect(Collectors.groupingBy(TpDefEB::getTyCode));
        Set<Map.Entry<String, List<TpDefEB>>> entrySet = groupList.entrySet();
        Map<String, Map<String, String>> result = new HashMap<>();
        for (Map.Entry<String, List<TpDefEB>> entry : entrySet) {
            String tyCode = entry.getKey().substring(2);
            Map<String, String> map = new HashMap<>();
            List<TpDefEB> ebList = entry.getValue();
            for (TpDefEB eb : ebList) {
                map.put(eb.getTpCode().substring(2), eb.getTpValue());
            }
            result.put(tyCode, map);
        }
        System.out.println(result);
    }

    /**
     * 字典翻译  list>Map<String, Map<String, String>>
     * groupingBy写法
     */
    @Test
    public void f3() {
        Map<String, Map<String, String>> map = tpDefList.stream()
                .peek(x -> {
                    x.setTpCode(x.getTpCode().substring(2));
                    x.setTyCode(x.getTyCode().substring(2));
                })
                .collect(Collectors.groupingBy(TpDefEB::getTyCode, Collectors.toMap(TpDefEB::getTpCode, TpDefEB::getTpValue)));
        System.out.println(map);
    }


    /**
     * partitioningBy(predicate) 根据条件分成2组。
     * 一组满足条件，一组不满足条件。
     */
    @Test
    public void f4() {
        Map<Boolean, List<UserEB>> map = userEBList.stream()
                .collect(Collectors.partitioningBy(user -> user.getAge() >= 30));

        map.entrySet().stream()
                .forEach(System.out::println);
        /**结果：
         * false=[UserEB{name='古力娜扎', age=28}]
         * true=[UserEB{name='刘诗诗', age=30}, UserEB{name='刘亦菲', age=30}]
         */
    }

    /**
     * 将员工按性别和地区分类
     */
    @Test
    public void f5() {
        Map<String, Map<String, List<UserEB>>> map = userEBList2.stream()
                .collect(Collectors.groupingBy(UserEB::getSex, Collectors.groupingBy(UserEB::getAddress)));
        //输出结果
        Set<Map.Entry<String, Map<String, List<UserEB>>>> entrySet = map.entrySet();
        for (Map.Entry<String, Map<String, List<UserEB>>> mapEntry : entrySet) {
            System.out.println("------组=" + mapEntry.getKey());
            Map<String, List<UserEB>> map2 = mapEntry.getValue();
            Set<Map.Entry<String, List<UserEB>>> entrySet2 = map2.entrySet();
            for (Map.Entry<String, List<UserEB>> entry : entrySet2) {
                System.out.println(entry.getKey() + "------" + entry.getValue().stream().map(UserEB::getName).collect(Collectors.joining(",")));
            }
        }

    }

    @Test
    public void f6() {
        Map<String, String> map = userEBList2.stream()
                .collect(Collectors.toMap(UserEB::getName, UserEB::getAddress));
        System.out.println(map);
    }


    private void getUserEBList() {
        userEBList = new ArrayList<>();
        userEBList.add(new UserEB("刘诗诗", 30));
        userEBList.add(new UserEB("刘亦菲", 30));
        userEBList.add(new UserEB("古力娜扎", 28));
    }

    private void getUserEBList2() {
        userEBList2 = new ArrayList<>();
        userEBList2.add(new UserEB("刘诗诗", 33, 2000, "女", "北京"));
        userEBList2.add(new UserEB("刘亦菲", 33, 2500, "女", "武汉"));
        userEBList2.add(new UserEB("古力娜扎", 30, 1500, "女", "乌鲁木齐"));
        userEBList2.add(new UserEB("陈都灵", 29, 1500, "女", "武汉"));
        userEBList2.add(new UserEB("杨紫", 28, 1500, "女", "北京"));
        userEBList2.add(new UserEB("林青霞", 60, 3000, "女", "台北"));
        userEBList2.add(new UserEB("刘德华", 60, 3000, "男", "香港"));
    }


    private void getTpDefEBList() {
        tpDefList = new ArrayList<>();
        tpDefList.add(new TpDefEB("AA00", "女", "00AA"));
        tpDefList.add(new TpDefEB("AA01", "男", "00AA"));

        tpDefList.add(new TpDefEB("AB01", "P1", "00AB"));
        tpDefList.add(new TpDefEB("AB02", "P2", "00AB"));
        tpDefList.add(new TpDefEB("AB03", "P3", "00AB"));
        tpDefList.add(new TpDefEB("AB04", "P4", "00AB"));
    }

}
